//
//  AppDelegate.m
//  01-UIView、UIWindow
//
//  Created by QC.L on 16/7/14.
//  Copyright © 2016年 QC.L. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "UIView+Frame.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 程序的组成结构
    // 1.man.m 主要完成了程序的正常运行
    // 2.AppDelegate程序的执行者,签订了UIApplicationDelegate(协议方法可选)
    // 注意:AppDelegate 就是普通的类,只不过签订协议,才有了这么多方法.
    // 3.ViewController 视图控制器
    // 4. Main.sb(视图管理) launchScreen.sb(负责启动页) 可视化管理 (Xcode 7 以前叫 LaunchScreen.xib)
    // 5.Assets.xcassets 主要用来管理图片素材(xcode 7 以前叫 Images.xcassets)
    // 6.Info.plist (工程配置文件)
    // 模拟器
    // command + 1~5 改变 模拟器大小
    // command + 左右 横屏竖屏
    
    // 1.UIWindow (程序中只有一个主window)
    // 1> 取消程序的可视化入口,Main.sb  (Main interface 栏 删掉Main.storyboard 再回车)
    // 2> 创建主window对象
    //

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 给你创建的窗口上颜色
    _window.backgroundColor = [UIColor blackColor];
    // 让window成为主窗口,并显示
    [_window makeKeyAndVisible];
    // 从xcode 7 以后必须设置rootViewController ,否则crash
    _window.rootViewController = [[UIViewController alloc] init];
    
    // 3> UIWindow 继承UIView
    // 2. UIView (与NSObject相似,是UI阶段的根类,大部分UI控件继承UIView)
    // 学习新类
    // 1> 需要新类包含哪些初始化方法
    // 2>包含哪些行为(方法)和特征(属性)
#if 0
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
    view.backgroundColor = [UIColor greenColor];
    // 添加到window上
    [_window addSubview:view];
    // 遇到UIView的子类都可以用以上方式创建
    // iOS系统坐标系
    // 第四象限,而y的值为正
    // UIView 为矩形
    // frame bounds center(UIView 的属性)
    // 1.frame (x,y,width,height) 相对父视图的坐标系(x,y,width,height)
    // 2.bounds(x,y,width,height) 相对自身的坐标系(0,0,width,height)
    // 3.center 中心点
    NSLog(@"%@\ncenter:%@",NSStringFromCGRect(view.frame),NSStringFromCGPoint(view.center));
    view.bounds = CGRectMake(10, 10, 300, 300);
    NSLog(@"%@\ncenter:%@",NSStringFromCGRect(view.frame),NSStringFromCGPoint(view.center));
    
    // bounds的中心点不变,当改变bounds的长和宽时,改变后的x,y坐标是x改变量 / 2,y改变量 / 2
    // bounds (x,y)影响子视图的坐标系
//    UIView *subView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
//    subView.backgroundColor = [UIColor redColor];
//    [view addSubview:subView];
    // 改变绿色view的大小,width 变成 100
//    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
//    
//    CGRect rect = view.frame;
//    rect.size.width = 100;
//    view.frame = rect;
   // c 和 oc 的混搭,变成一个常量无法修改,所以不能直接赋值
    view.ymj_width = 100;
    // 单独改变就用类目,四个地方都改变就用CGRectMake
#endif
    //UIView 的基本属性
    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    // 颜色
    testView.backgroundColor = [UIColor blueColor];
    [_window addSubview:testView];
    // 移除(往数组添加,删除,引用计数相应+1,-1)
//    [testView removeFromSuperview];
    // 透明度 0.0 ~ 1.0
    testView.alpha = 0.8;
    // tag 获取view __kindof 当前类及其子类能用
    testView.tag = 27;
    [_window viewWithTag:27];
    // subViews 子视图们
    UIView *arrayView = [_window.subviews objectAtIndex:0];
    NSLog(@"%@",arrayView);
    // superView
    UIView *superView = testView.superview;
    NSLog(@"%@",superView);
    // hidden 隐藏/显示 默认是显示,即NO
    testView.hidden = NO;
    // 视图层级
    // 到指定下标之上
    // 到指定下标
    // 到指定下标之上
//    [testView insertSubview:<#(nonnull UIView *)#> aboveSubview:<#(nonnull UIView *)#>]
//    [testView insertSubview:<#(nonnull UIView *)#> atIndex:<#(NSInteger)#>]
//    [testView insertSubview:<#(nonnull UIView *)#> belowSubview:<#(nonnull UIView *)#>]
    UIView *firView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    firView.backgroundColor = [UIColor yellowColor];
    UIView *secView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];
    secView.backgroundColor = [UIColor redColor];
    UIView *lastView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
    lastView.backgroundColor = [UIColor purpleColor];
    //绿 (1)textView
    //黄 (2)firView
    //红 (3)secView
    [_window addSubview:firView];
//    [_window addSubview:secView];
    [_window insertSubview:secView atIndex:2];
    //绿 (1)textView
    //红 (2)secView
    //黄 (3)firView
    // atIndex 插入后会定去别的位置,后面的视图会往后排序
//    [_window addSubview:lastView];
//    (正常添加lastView子类,lastView会在firstView上面)
    //紫 (4)lastView
//    (调用aboveSubView 不变化)
//    [_window insertSubview:lastView aboveSubview:firView];
    //绿 (1)textView
    //红 (2)secView
    //黄 (3)firView
    //紫 (4)lastView
//    (调用belowSubView 会在firView下面)
    [_window insertSubview:lastView belowSubview:firView];
    //绿 (1)textView
    //红 (2)secView
    //紫 (3)lastView
    //黄 (4)firView
    
    
    // label 的属性
    // 1.text
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 80, 30)];
    label.backgroundColor = [UIColor whiteColor];
    [_window addSubview:label];
    label.text = @"jorcesghfsjgdngdngfhfyjtyjyukyukyukyukyukyu";
    // 2.textColor
    label.textColor = [UIColor blackColor];
    // 3.font systemFontOfSize 默认17
    label.font = [UIFont systemFontOfSize:20];
    // 加粗
//    label.font = [UIFont boldSystemFontOfSize:20];
    // 4.textAlignment 对齐方式
    /**default is NSTextAlignmentLeft = 0,
     NSTextAlignmentCenter    = 1,   
     NSTextAlignmentRight     = 2,
     */
    label.textAlignment = 1;
    // 5.number
    label.numberOfLines = 0;
    // 自适应
    [label sizeToFit];
    // 限制N行自适应
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 30, 100, 21)];
    textLabel.text = @"dffgghjhjshhjshsdhgsdhgs";
    textLabel.backgroundColor = [UIColor whiteColor];
    textLabel.numberOfLines = 3;
    [textLabel sizeToFit];
    
    // UIButton 继承UIControl 继承UIView
    UIButton *button= [UIButton buttonWithType:UIButtonTypeSystem];
    // 别忘了给上坐标
    button.frame = CGRectMake(0, 300, 80, 50);
    button.backgroundColor = [UIColor cyanColor];
    [_window addSubview:button];
    // 添加事件
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    // 添加文字
    [button setTitle:@"打我呀" forState:UIControlStateNormal];
    // 文字颜色设置
    [button setTitleColor:[UIColor whiteColor] forState:0];
    // UITextField 继承UIControl 继承UIView
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(150, 300, 120, 50)];
    textField.backgroundColor  = [UIColor whiteColor];
    [_window addSubview:textField];
    // 默认有的文字
    textField.text = @"文字";
    // 站位字符(文字为空的默认提示信息,当文字不为空时,站位信息不显示)
    textField.placeholder = @"提示信息";
    // 文字颜色
    textField.textColor = [UIColor greenColor];
    // 边沿 风格
//    textField.borderStyle = UITextBorderStyleBezel;
    // 带圆角的风格
    textField.borderStyle = UITextBorderStyleRoundedRect;
    // clearButtonMode 清空一切编辑内容,默认是NO
    textField.clearsOnBeginEditing = YES;
    // command + k 收起,回收键盘
    return YES;
}
- (void)buttonClick{
    NSLog(@"我被人打了,555");
    [_window endEditing:YES];
    // 结束编辑;
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
